Skip to main content

Service Methods Operation/Flows


1. shorten(originalUrl: string): Promise of { shortUrl: string }

Purpose: Generate a shortened version of the given original URL. If the URL already exists in the database, return the existing shortened version.

Flow:

  • Start

  • Search for an existing ShortUrl entity by originalUrl

    • If found → format and return existing short URL
  • Else:

    • Generate a new short code using nanoid(8)
    • Create a new ShortUrl entity with originalUrl and shortCode
    • Save the new entity
    • Format and return the new short URL

2. resolve(shortCode: string): Promise of string or null

Purpose: Resolve a short code back to the original full URL.

Flow:

  • Start

  • Look up ShortUrl entity by shortCode

    • If found → return originalUrl
    • Else → return null

3. formatShortUrl(domain: string, shortCode: string): { shortUrl: string }

Purpose: Generate a properly formatted short URL using the domain and short code.

Flow:

  • Trim trailing slashes from domain
  • Append /su/ followed by the shortCode
  • Return the full shortUrl as an object

Note: This is a private utility method used internally in the service.